﻿#include "ordertimesdlg.h"
#include "ui_ordertimesdlg.h"
#include<QComboBox>
#include"ufxconfig.h"
#include<QMessageBox>

OrderTimesDlg::OrderTimesDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OrderTimesDlg)
{
    ui->setupUi(this);

    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setRowCount(3);
    ui->tableWidget->setColumnWidth(0,80);
    ui->tableWidget->setColumnWidth(1,353);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem(QStringLiteral("快捷键")));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem(QStringLiteral("数量乘数")));
    ui->tableWidget->setAlternatingRowColors(true);

    for(int i = 0; i < 3; i++){
        QComboBox* m_KeyCombox = new QComboBox();
        m_KeyCombox->addItem(QStringLiteral("1"));//0
        m_KeyCombox->addItem(QStringLiteral("2"));//1
        m_KeyCombox->addItem(QStringLiteral("3"));
        m_KeyCombox->addItem(QStringLiteral("4"));
        m_KeyCombox->addItem(QStringLiteral("5"));
        m_KeyCombox->addItem(QStringLiteral("6"));
        m_KeyCombox->addItem(QStringLiteral("7"));
        m_KeyCombox->addItem(QStringLiteral("8"));
        m_KeyCombox->addItem(QStringLiteral("9"));
        m_KeyCombox->addItem(QStringLiteral("0"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 1"));//10
        m_KeyCombox->addItem(QStringLiteral("小键盘 2"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 3"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 4"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 5"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 6"));//15
        m_KeyCombox->addItem(QStringLiteral("小键盘 7"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 8"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 9"));
        m_KeyCombox->addItem(QStringLiteral("小键盘 0"));
        m_KeyCombox->addItem(QStringLiteral("A"));//20
        m_KeyCombox->addItem(QStringLiteral("B"));
        m_KeyCombox->addItem(QStringLiteral("C"));
        m_KeyCombox->addItem(QStringLiteral("D"));
        m_KeyCombox->addItem(QStringLiteral("E"));
        m_KeyCombox->addItem(QStringLiteral("F"));
        m_KeyCombox->addItem(QStringLiteral("G"));
        m_KeyCombox->addItem(QStringLiteral("H"));
        m_KeyCombox->addItem(QStringLiteral("I"));
        m_KeyCombox->addItem(QStringLiteral("J"));
        m_KeyCombox->addItem(QStringLiteral("K"));
        m_KeyCombox->addItem(QStringLiteral("L"));
        m_KeyCombox->addItem(QStringLiteral("M"));
        m_KeyCombox->addItem(QStringLiteral("N"));
        m_KeyCombox->addItem(QStringLiteral("O"));
        m_KeyCombox->addItem(QStringLiteral("P"));
        m_KeyCombox->addItem(QStringLiteral("Q"));
        m_KeyCombox->addItem(QStringLiteral("R"));
        m_KeyCombox->addItem(QStringLiteral("S"));
        m_KeyCombox->addItem(QStringLiteral("T"));
        m_KeyCombox->addItem(QStringLiteral("U"));
        m_KeyCombox->addItem(QStringLiteral("V"));
        m_KeyCombox->addItem(QStringLiteral("W"));
        m_KeyCombox->addItem(QStringLiteral("X"));
        m_KeyCombox->addItem(QStringLiteral("Y"));
        m_KeyCombox->addItem(QStringLiteral("Z"));
        m_KeyCombox->addItem(QStringLiteral("F1"));
        m_KeyCombox->addItem(QStringLiteral("F2"));
        m_KeyCombox->addItem(QStringLiteral("F3"));
        m_KeyCombox->addItem(QStringLiteral("F4"));
        m_KeyCombox->addItem(QStringLiteral("F5"));
        m_KeyCombox->addItem(QStringLiteral("F6"));
        m_KeyCombox->addItem(QStringLiteral("F7"));
        m_KeyCombox->addItem(QStringLiteral("F8"));
        m_KeyCombox->addItem(QStringLiteral("F9"));
        m_KeyCombox->addItem(QStringLiteral("F10"));
        m_KeyCombox->addItem(QStringLiteral("F11"));
        m_KeyCombox->addItem(QStringLiteral("F12"));
        m_KeyCombox->addItem(QStringLiteral("空格键"));
        m_KeyCombox->addItem(QStringLiteral("回车键"));
        m_KeyCombox->addItem(QStringLiteral("Alt键"));
        m_KeyCombox->addItem(QStringLiteral("Ctrl键"));
        m_KeyCombox->addItem(QStringLiteral("↑"));
        m_KeyCombox->addItem(QStringLiteral("↓"));
        m_KeyCombox->addItem(QStringLiteral("←"));
        m_KeyCombox->addItem(QStringLiteral("→"));
        m_KeyCombox->addItem(QStringLiteral("减号"));
        m_KeyCombox->addItem(QStringLiteral("加号"));
        m_KeyCombox->addItem(QStringLiteral("等号"));
        m_KeyCombox->addItem(QStringLiteral("乘号"));
        m_KeyCombox->addItem(QStringLiteral("除号"));
        m_KeyCombox->addItem(QStringLiteral("删除键"));
        m_KeyCombox->addItem(QStringLiteral("插入键"));

        ui->tableWidget->setCellWidget(i, 0, (QWidget*)m_KeyCombox);

        connect(m_KeyCombox,SIGNAL(currentTextChanged(QString)),this,SLOT(SlotCurrentKeyChanged(QString)));
    }

    //从配置文件中读取预设内容
    QString value = UFXConfig::getInstance()->GetString("Default","OrderTimes","");
    if(value != 0){
        QStringList list1 = value.split("|");
        if(list1.size() == 3){
            for(int i = 0;i<3; i++){
                QStringList list2 = list1[i].split(",");
                if(list2.size()==2){
                    ((QComboBox*)ui->tableWidget->cellWidget(i,0))->setCurrentText(list2[0]);
                    ui->tableWidget->setItem(i,1,new QTableWidgetItem(list2[1]));
                    m_OrderTimes.insert(list2[0],list2[1].toInt());
                }
            }
        }
    }
}

OrderTimesDlg::~OrderTimesDlg()
{
    delete ui;
}

int OrderTimesDlg::GetOrderTimes(QString key){
    if(m_OrderTimes.contains(key)){
        return m_OrderTimes[key];
    }
    return -1;
}
void OrderTimesDlg::SlotJudgeTimesKeyString(QString msg){
    QMessageBox::critical(this,QString("Error"),msg);
}
void OrderTimesDlg::SlotCurrentKeyChanged(QString key){
    int iCnt = 0;
    for(int i = 0; i < 3; i++){
        QString keyString = ((QComboBox*)ui->tableWidget->cellWidget(i,0))->currentText();
        if(key == keyString){
            iCnt++;
        }
    }
    if(iCnt > 1){
        QString msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 下单数量倍率 中使用! ");
        QMessageBox::critical(this,QString("Error"),msg);
        return;
    }else{
        emit SigJudgeTimesKeyString(key);
    }
}

bool OrderTimesDlg::JudgeKeyStringFlg(QString key){
    return m_OrderTimes.contains(key);
}
void OrderTimesDlg::OKButtonClicked(){
    m_OrderTimes.clear();
    //将设置好的快捷键保存到配置文件中
    QString value = "";
    int cnt = ui->tableWidget->rowCount();
    for(int i =0; i <cnt; i++){
        QString keyStr = ((QComboBox*)ui->tableWidget->cellWidget(i,0))->currentText();
        QString times = ui->tableWidget->item(i,1)->text();

        QString tmp = keyStr +"," +times;
        if(i == 0){
            value = tmp;
        }else{
            value = value +"|"+ tmp;
        }
        m_OrderTimes.insert(keyStr,times.toInt());
    }
    UFXConfig::getInstance()->SetString("Default","OrderTimes",value.toStdString().c_str());
}
